<template>
	<uv-popup ref="sharePpopup" mode="bottom" @maskClick="handleClosePopup">
		<view class="layout-share-popup">
			<view class="share-box">
				<view class="share-item">
					<image class="img" src="/static/images/share-wx.png"></image>
					<view class="tit">分享微信好友</view>
					<button class="button" open-type="share"></button>
				</view>
				<view class="share-item" @click="handleSharePoster">
					<image class="img" src="/static/images/share-poster.png"></image>
					<view class="tit">生成海报分享</view>
				</view>
			</view>
			<view class="line"></view>
			<view class="cancel" @click="handleClosePopup">取消</view>
		</view>
	</uv-popup>
</template>
<script setup lang="ts">
	import { watch, ref } from 'vue';
	import { queryShare } from '@/core/api'
	import { UserToken, shareUrl } from '@/core/consts'
	import { handleShowToast, handleRouteNavigateTo } from '@/core/utils'
	import { ShareModelEnum, ShareClientEnum, ShareEnum } from '@/core/enum'
	import { queryParams } from '@/uni_modules/uv-ui-tools/libs/function/index.js';

	const sharePpopup = ref();

	const props = withDefaults(defineProps<{
		goodsDetailData : any,
		show : boolean,
		shareType : number,
	}>(), {
		goodsDetailData: {},
		show: false,
		shareType: 1,
	});

	const emits = defineEmits(['handleClosePopup'])

	watch(() => props.show, (newVal : boolean) => {
		if (newVal) {
			sharePpopup.value.open();
		} else {
			sharePpopup.value.close();
		}
	});

	const handleClosePopup = () => {
		emits('handleClosePopup');
	}

	/** 海报分享 */
	const handleSharePoster = async () => {
		uni.showLoading({
			title: '加载中',
		});
		let data : any = {
			page: props.shareType,
			url: shareUrl,
			params: {},
			type: ShareModelEnum.poster,
			client: ShareClientEnum.wxMiNiProgram
		}
		if (uni.getStorageSync(UserToken)) {
			data['token'] = uni.getStorageSync(UserToken)
		}
		if (props.shareType === ShareEnum.pinTuan || props.shareType === ShareEnum.addPinTuan) {
			data.params = {
				groupId: props.goodsDetailData?.pinTuanRule.id,
				goodsId: props.goodsDetailData.id,
				teamId: props.goodsDetailData.teamId || 0,
			}
		} else if (props.shareType === ShareEnum.group || props.shareType === ShareEnum.seckill) {
			data.params = {
				groupId: props.goodsDetailData?.groupId
			}
		} else if (props.shareType === ShareEnum.goods) {
			data.params = {
				goodsId: props.goodsDetailData?.id
			}
		} else if (props.shareType === ShareEnum.solitaire) {
			data.params = {
				id: props.goodsDetailData?.id
			}
		} else if (props.shareType === ShareEnum.serviceGoods) {
			data.params = {
				serviceGoodsId: props.goodsDetailData?.id
			}
		}
		const share = await queryShare(data);
		if (share.status) {
			uni.hideLoading();
			emits('handleClosePopup');
			if (props.shareType === ShareEnum.goods ||
				props.shareType === ShareEnum.pinTuan ||
				props.shareType === ShareEnum.addPinTuan ||
				props.shareType === ShareEnum.seckill ||
				props.shareType === ShareEnum.group ||
				props.shareType === ShareEnum.serviceGoods
			) {
				data.params.posterUrl = share.data;
				data.params.shareType = props.shareType;
				handleRouteNavigateTo(`/pages/share/sharePoster/sharePoster` + queryParams(data.params));
			} else {
				handleRouteNavigateTo(`/pages/share/qrCode/qrCode?poster=${encodeURIComponent(share.data)}`)
			}
		} else {
			uni.hideLoading();
			handleShowToast(share.msg);
		}
	}
</script>
<style lang="scss" scoped>
	@import './coreshop-share.scss';
</style>